<template>
  <div>
    <div v-if="searchStatus" class="box-shadow pad10 mgb10 flex jbetween">
      <div>
        <el-form :model="queryParams" ref="queryRef" :inline="true">
          <el-form-item label="网课类型">
            <Selector v-model="queryParams.Type" category="网课类型"></Selector>
          </el-form-item>
          <el-form-item label="网课名称">
            <el-input
              v-model="queryParams.Name"
              placeholder="请输入"
              clearable
              style="width: 212px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-form>
      </div>
      <div class="flex acenter jend fnw">
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </div>
    </div>
    <MyTable
      :tableList="tableList"
      :total="Total"
      :columns="columns"
      v-model:page="page"
      v-model:searchStatus="searchStatus"
      :btnList="$checkBtn(['OnlinePlatform:edit', 'OnlinePlatform:delete'])"
      @getList="getList"
      @edit="handleEdit"
      @delete="handleDelete"
    >
      <template v-slot:tabTop>
        <el-button
          type="primary"
          v-hasPermi="['OnlinePlatform:add']"
          icon="CirclePlusFilled"
          @click="handleAdd"
          >添加网课</el-button
        >
      </template>
    </MyTable>
    <MyDialog
      v-model:show="show"
      :title="title"
      :formRef="formRef"
      :width="500"
      @confirm="handleConfirm"
    >
      <el-form
        :model="formData"
        :rules="rules"
        inline
        :label-width="80"
        ref="formRef"
        class="wid100"
      >
        <el-form-item label="视频封面" prop="VideoCover">
          <FileUpload
            :iconList="['preview']"
            type="img"
            :limt="1"
            :detail="false"
            :multiple="false"
            v-model:fileList="formData.VideoCover"
          ></FileUpload>
        </el-form-item>
        <el-form-item prop="ID_Type" label="网课类型">
          <Selector v-model="formData.ID_Type" category="网课类型"></Selector>
        </el-form-item>
        <el-form-item label="网课名称" prop="Name">
          <el-input v-model="formData.Name" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="视频文件" prop="VideoPath">
          <FileUpload
            :limt="1"
            :detail="false"
            :showFile="false"
            :limitType="[
              '.mp4',
              '.wmv',
              '.asf',
              '.asx',
              '.rm',
              '.rmvb',
              '.3gp',
              '.m4v',
              '.mov',
              '.avi',
              '.dat',
              '.mkv',
              '.flv',
              '.vob'
            ]"
            :multiple="false"
            v-model:fileList="formData.VideoPath"
          ></FileUpload>
          <video
            class="mgt10"
            width="300"
            height="200"
            controls
            v-if="formData.VideoPath && formData.VideoPath.length"
            :src="formData.VideoPath[0].Path"
          ></video>
        </el-form-item>
        <el-form-item label="视频简介" prop="Introduce">
          <el-input
            type="textarea"
            v-model="formData.Introduce"
            placeholder="请输入"
            clearable
          />
        </el-form-item>
      </el-form>
    </MyDialog>
  </div>
</template>

<script setup name="OnlinePlatform">
import { ref, getCurrentInstance, onMounted } from 'vue';
import {
  DelCourse,
  AddViewCounts,
  AddOrEditCourse,
  GetCourseList_Page,
  GetCourseDetail
} from '@/api/online/platform.js';

const { proxy } = getCurrentInstance();
const tableList = ref([]);
const Total = ref(0);
const columns = ref([
  { label: '序号' },
  { label: '网课类型', prop: 'Name_Type' },
  { label: '网课名称', prop: 'Name' },
  { label: '简介', prop: 'Introduce' },
  { label: '观看次数', prop: 'ViewCounts' },
  { label: '视频封面', prop: 'VideoCover', img: true },
  { label: '创建时间', prop: 'CreateTime' },
  { label: '创建人', prop: 'CreateUser', user: true },
  { label: '操作', width: 160 }
]);
proxy.$formatKey(columns.value);
const page = ref({
  PageIndex: 1,
  PageSize: 10
});
const showVideoUrl = ref('');
const searchStatus = ref(true);
const queryParams = ref({
  Name: '',
  Type: '',
  SortOrder: 'CreateTime DESC',
  NoPage: '' //如果传"true"，则返回全部数据不分页
});
const queryRef = ref();
const show = ref(false);
const title = ref();
const formRef = ref();
const rules = ref({
  ID_Type: [{ required: true, message: '请选择', trigger: 'change' }],
  Name: [{ required: true, message: '请输入', trigger: 'blur' }],
  VideoCover: [{ required: true, message: '请选择', trigger: 'change' }],
  VideoPath: [{ required: true, message: '请选择', trigger: 'change' }]
});
const formData = ref({});

onMounted(() => {
  getList();
});

async function getList() {
  try {
    let query = {
      ...queryParams.value,
      ...page.value
    };
    let res = await GetCourseList_Page(query);
    tableList.value = res.data.List;
    Total.value = res.data.Total;
  } catch (error) {
    console.log(error);
  }
}

async function handleConfirm(val) {
  if (val) {
    try {
      console.log(formData.value);
      let query = {
        ...formData.value,
        showVideoUrl: undefined,
        VideoCover: proxy.$removeFileHead(
          formData.value?.VideoCover?.[0]?.Path
        ),
        VideoPath: proxy.$removeFileHead(formData.value?.VideoPath?.[0]?.Path)
      };
      await AddOrEditCourse(query);
      proxy.$modal.msgSuccess('提交成功');
      show.value = false;
      getList();
    } catch (error) {
      console.log(error);
    }
  }
}

function handleEdit(row) {
  title.value = '修改网课';
  formData.value = {
    ID_Type: row.ID_Type,
    Name: row.Name,
    Introduce: row.Introduce,
    VideoCover: [
      {
        Suffix: '.png',
        Path: proxy.$addFileHead(row.VideoCover)
      }
    ],
    VideoPath: [
      {
        Path: proxy.$addFileHead(row.VideoPath)
      }
    ]
  };
  show.value = true;
}
async function handleDelete(row) {
  try {
    await DelCourse({ ID: row.ID });
    proxy.$modal.msgSuccess('删除成功');
    getList();
  } catch (error) {
    console.log(error);
  }
}
function handleAdd() {
  title.value = '添加网课';
  formData.value = {};
  show.value = true;
}
function handleQuery() {
  page.value.PageIndex = 1;
  getList();
}
function resetQuery() {
  queryParams.value = {
    Name: '',
    NoPage: '',
    Type: '', //网课类型ID,
    SortOrder: 'CreateTime DESC'
  };
  handleQuery();
}
</script>

<style lang="scss" scoped></style>
